﻿// Custom example logic
var uploader1;
var gFileArr1 = new Array();
var gInterval1;
var gReturnServerData = "";
var gIsEdit = false;
var gShowSubjectSelect = false
function saveGroup()
{
	var gn = $("#txtGroupName").val();

	if(gn!="" && (gReturnServerData!="" || gIsEdit==true))
	{
		$.post("?act=filegroup&op=ajaxSaveFile",{fileData:gReturnServerData,group:gn,id:curDirSid},function(data){
			
			//alert("333333"+data);
	    	 var j = eval("("+data+")");
			 if(j.st=="ok")
			 {
				 //$( "#dialog-form" ).dialog("close");
				 //gReturnServerData = "";
				 if(gIsEdit)
				 {
					 alert("修改成功");
					 lstDir();
				 }
				 else
				 {
					 gShowSubjectSelect = true;
					 lstDir();
					 if(confirm("创建成功  继续上传素材？"))
					 {
						  $( "#dialog-form-add-subject" ).dialog("close");
						 if(refreshUpload())
						{
							$("#dialog-form").dialog("open");
						}
					 }
					 else
					 {
						 $( "#dialog-form-add-subject" ).dialog("close");
					 }
					 
				 }
				
			 }
			 else { alert(j.msg);}
		});
	}
	else
	{
		if(gId==0)
		{
			alert("请选择图片并输入链接主题");
		}
		else
		{
			alert("保存成功");
		}
		
	}
}
 
function checkFinish1()
{
	var haveAllTure = true;
	for(var item in gFileArr1)
	{
	    if(!gFileArr1[item])
		{
			haveAllTure = false;
			break;
		}
	}
	if(haveAllTure)
	{
		clearInterval(gInterval1);
		//alert("上传完毕");
		
		$("#dialog-confirm").find("#msg").html("处理完毕");
		$("#dialog-confirm").dialog("close");
		//alert("上传完毕");
		//headerShowTips("感谢您的上传 继续上传文件吗？",1);
	}
}
function initProgrssBar(jo)
{
	var progressbar = jo;
	progressbar.progressbar({
      value: 0
    });
	 
}
$(function() {
			initUploader1();
    $( "#dialog-form-add-subject" ).dialog({
			autoOpen: false,
			height:500,
			width: 500,
			modal: true,
			buttons: {
				"保存": function() {
 					 saveGroup();
				},
				"关闭": function() {
 					$( "#dialog-form-add-subject" ).dialog("close");
				}
			},
			close: function() {
			}
		});
		$("#btnAddFengMian").click(function(){
            gShowSubjectSelect = false;
			$.get("?act=filegroup&op=getSubjectInfo&id="+curDirSid,function(d){
				var j = eval("("+d+")");
				if(j.path!=null)
				{
					$("#imgUpload")[0].src=j.path;
					$("#imgUpload").show();
					$("#txtGroupName").val(j.name);
					gIsEdit = true;
					$("#idSubjectTips").hide();
					$("#btnSubjectModify").show();
					$("#btnSubjectModify1").show();
				}
				else
				{
					$("#imgUpload").hide();
					$("#txtGroupName").val("");
					gIsEdit = false;
					$("#btnSubjectModify").hide();
					$("#btnSubjectModify1").hide();
				}
				})
		$( "#dialog-form-add-subject" ).dialog("open");	
		});
$("#txtGroupName").keyup(function(){
	   if(!gIsEdit)
	   {
	   	   // showSubjectTips();
	   }
	});

});
function refreshUpload1()
{
       //$("#ui-id-1").html("添加超大附件");
		$("#filelist1").hide();
		document.getElementById('filelist1').innerHTML ="";
 		uploader1.refresh();
		return true;
}
 
 
//////////////////////////////

function initUploader1()
{
	uploader1 = new plupload.Uploader({
	runtimes : 'html5,flash,silverlight,html4',
	browse_button : 'pickfiles1', // you can pass in id...
	container: document.getElementById('dialog-form-add-subject'), // ... or DOM Element itself
	url : 'upload.php',
	flash_swf_url : 'js/Moxie.swf',
	silverlight_xap_url : 'js/Moxie.xap',
	chunk_size:"1mb",
	filters : {
		max_file_size : '5000mb',
		mime_types: [
			{title : "Image files", extensions : "jpg,gif,png"}
		]
	},

	init: {
		PostInit: function() {
			document.getElementById('filelist1').innerHTML = ''; 
		},

		FilesAdded: function(up, files) {
			plupload.each(files, function(file) {
				//initProgrssBar();
				var html = $("#divTempFileHtml").html();
				html = html.replace("TBLID","tblFile_"+file.id);
				var str = file.name;
				if(str.length>30)
				{
					str = str.substring(0,30)+"...";
				}
				html = html.replace("NAME",str);
				html = html.replace("SIZE",plupload.formatSize(file.size));
				document.getElementById('filelist1').innerHTML += "<li>"+html+"</li>";
				//alert(html);
				$("#tblFile_"+file.id).find("#progressbar").progressbar({value:0});
				if($("#filelist1").is(":hidden"))
				{
					$("#filelist1").show();
				}
				uploader1.start();
 
			});
		},

		UploadProgress: function(up, file) {
			//document.getElementById("tdProess_"+file.id).innerHTML = '<span>' + file.percent + "%</span>";
			$("#tblFile_"+file.id).find("#progressbar").progressbar({value:file.percent});
			//$("#tblFile_"+file.id).find("#progressbar").progressbar("value",file.percent);
			//document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
		},
        FileUploaded:function(up,file,o)
		{
			//alert(o);
			//$("#dialog-confirm").dialog("open");
			var json = eval("("+o.response+")");
			$("#tblFile_"+file.id).find("#imgOk").show();
			gFileArr1[file.id] = false;
			$.post("?act=filegroup&op=saveFile",{name:file.name,path:json.path,dirId:curDirId,dirName:curDirName,fid:file.id},function(data){
				try
				{
				var j= eval("("+data+")");
				if(j.st=="ok")
				{
					$("#imgUpload").show();
					$("#imgUpload")[0].src=j.path;
					gReturnServerData = j.data;
					gFileArr1[j.fid] = true;
					refreshUpload1();
				}
				}
				catch(e){}
				});
			gInterval1 = setInterval("checkFinish1()",1000);
		},
		UploadComplete:function(up,o)
		{
			//alert("全部上传完毕");
			
		},
		Error: function(up, err) {
			document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
		}
	}
});
	uploader1.init();
}
function checkFinish1()
{
	var haveAllTure = true;
	for(var item in gFileArr1)
	{
	    if(!gFileArr1[item])
		{
			haveAllTure = false;
			break;
		}
	}
	if(haveAllTure)
	{
		clearInterval(gInterval1);
		//alert("上传完毕");
		
		$("#dialog-confirm").find("#msg").html("处理完毕");
		$("#dialog-confirm").dialog("close");
		//alert("上传完毕");
		//headerShowTips("感谢您的上传 继续上传文件吗？",1);
	}
}
function initProgrssBar(jo)
{
	var progressbar = jo;
	progressbar.progressbar({
      value: 0
    });
	 
}
function showSubjectTips()
{
	$("#idSubjectTips").show();
	$.get("?act=filegroup&op=getTips",function(d){
		var j = eval("("+d+")");
		$("#ulSubjectTips").html("");
		if(j.length==0)
		{
			$("#ulSubjectTips").html("<li>没有可设置的专题</li>");
		}
		else
		{
		for(i=0;i<j.length;i++)
		{
			var x = j[i];
			$("#ulSubjectTips").append("<li title='"+j[i]+"'>"+x+"</li>");
		}
		}
		$("#idSubjectTips").find("li").each(function(){
		$(this).click(function(){
		 
		 	$("#txtGroupName").val($(this).html());
  		 	$("#idSubjectTips").hide();
			});
		});
	
});
}